const urldata = require('./data');

const http = require('http');
const url = require('url');
const fs = require('fs');
const querystring = require('querystring');

let server = http.createServer((req, res) => {
    res.setHeader("Access-Control-Allow-Origin", "*");
    res.writeHead(200, {
        'Content-Type': 'text/plain;charset=uft-8;',
        'Access-Control-Allow-Origin': '*'
    })
    if (req.method === 'GET' || req.method === 'get') {
        // 请求的地址 path_
        var path_ = url.parse(req.url).pathname
        // 前端发送给给后端的数据
        var params = querystring.parse(url.parse(req.url).query)
        switch (path_) {
            case '/playlist/catlist':
                console.log("getSongListCategories");
                res.end(JSON.stringify(urldata.getSongListCategories()));
                break;
            case '/top/playlist':
                let sorts = params.sort;
                console.log("params:", params);
                if (sorts === 'new') {
                    res.end(JSON.stringify(urldata.getSongLists()));
                } else {
                    res.end(JSON.stringify(urldata.getSongLists2()));
                }
                break;
            case '/playlist/detail':
                console.log("getSongListDetail");
                console.log("/playlist/detail:", params);
                res.end(JSON.stringify(urldata.getSongListDetail()));
                break;
            case '/lyric':
                //getLyric?songmid=003rJSwm3TechU
                // isFormat: 是否格式化歌词, 默认值为 false
                let id = params.id;
                console.log("lyric:", params);
                res.end(JSON.stringify(urldata.getLyric1()));
                break;
            case '/song/url':
                // 必选参数
                // id: 歌曲id, 多个播放链接使用 ,分隔
                console.log("song/url:", params);
                res.end(JSON.stringify(urldata.getMusicPlay()));
                break;
            // case '/getImg':
            //     let sort = params.sort
            //     let img = params.img
            //     let path = `./images/${sort}/${img}`
            //     const data = fs.readFile(path, function (err, data) {
            //         if (err) {
            //             console.log(err);
            //             return;
            //         } else {
            //             res.write(data, 'binary');
            //             res.end();
            //         }
            //     })
            //     break;
            default:
                break;
        }
    }

    if (req.method === 'POST' || req.method === 'post') {
        // 请求的地址 path_
        var path_ = url.parse(req.url).pathname
        switch (path_) {
            case '/getSongLists':
                var postData = "";
                // 数据块接收中
                req.addListener("data", function (postDataChunk) {
                    postData += postDataChunk;
                });

                // 数据接收完毕，执行回调函数
                req.addListener("end", function () {
                    var sortId = "", params;
                    params = querystring.parse(postData);
                    sortId = params.sortId;
                    console.dir(params);
                    res.end(JSON.stringify(getSongLists()));
                });
                break;
            case '/user/saveOrUpdate':
                let buffer = Buffer.from([])
                req.on('data', (data) => {
                    buffer += data
                    saveUser(buffer.toString(), (result) => {
                        res.end(JSON.stringify(result))
                    })
                })
                req.on('end', () => { })
                break;
        }

    }
})

server.listen(3300, () => {
    console.log('启动成功')
})